﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TST.SharePointObjects
{
    sealed class ObjectReferenceComparer
    {
        private static IComparer<ObjectReference> _compareByName = new _sortName(false);
        public static IComparer<ObjectReference> CompareByName { get { return _compareByName; } }

        private static IComparer<ObjectReference> _compareByNameDesc = new _sortName(true);
        public static IComparer<ObjectReference> CompareByNameDesc { get { return _compareByNameDesc; } }

        private class _sortName : IComparer<ObjectReference>
        {
            bool _reverse;
            public _sortName(bool reverse)
            {
                this._reverse = reverse;
            }

            public int Compare(ObjectReference x, ObjectReference y)
            {
                if (_reverse) return y.Name.CompareTo(x.Name);
                else return x.Name.CompareTo(y.Name);
            }
        }

        private static IComparer<ObjectReference> _compareByType = new _sortType(false);
        public static IComparer<ObjectReference> CompareByType { get { return _compareByType; } }

        private static IComparer<ObjectReference> _compareByTypeDesc = new _sortType(true);
        public static IComparer<ObjectReference> CompareByTypeDesc { get { return _compareByTypeDesc; } }

        private class _sortType : IComparer<ObjectReference>
        {
            bool _reverse;
            public _sortType(bool reverse)
            {
                this._reverse = reverse;
            }

            public int Compare(ObjectReference x, ObjectReference y)
            {
                string xType = x.ReferenceTypeDescription + x.Name;
                string yType = y.ReferenceTypeDescription + y.Name;
                if (_reverse) return yType.CompareTo(xType);
                else return xType.CompareTo(yType);
            }
        }

        private static IComparer<ObjectReference> _compareBySite = new _sortSite(false);
        public static IComparer<ObjectReference> CompareBySite { get { return _compareBySite; } }

        private static IComparer<ObjectReference> _compareBySiteDesc = new _sortSite(true);
        public static IComparer<ObjectReference> CompareBySiteDesc { get { return _compareBySiteDesc; } }

        private class _sortSite : IComparer<ObjectReference>
        {
            bool _reverse;
            public _sortSite(bool reverse)
            {
                this._reverse = reverse;
            }

            public int Compare(ObjectReference x, ObjectReference y)
            {
                string xSite = x.SiteTitle + x.Name;
                string ySite = y.SiteTitle + y.Name;
                if (_reverse) return ySite.CompareTo(xSite);
                else return xSite.CompareTo(ySite);
            }
        }

        private static IComparer<ObjectReference> _compareBylist = new _sortlist(false);
        public static IComparer<ObjectReference> CompareBylist { get { return _compareBylist; } }

        private static IComparer<ObjectReference> _compareBylistDesc = new _sortlist(true);
        public static IComparer<ObjectReference> CompareBylistDesc { get { return _compareBylistDesc; } }

        private class _sortlist : IComparer<ObjectReference>
        {
            bool _reverse;
            public _sortlist(bool reverse)
            {
                this._reverse = reverse;
            }

            public int Compare(ObjectReference x, ObjectReference y)
            {
                string xlist = x.ListTitle + x.Name;
                string ylist = y.ListTitle + y.Name;
                if (_reverse) return ylist.CompareTo(xlist);
                else return xlist.CompareTo(ylist);
            }
        }

    }

}
